<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<div id="page-content">
    <div class="row">

        <div class="col-lg-12">
            <div class="panel">
                <div class="panel-heading">
                    <h3 class="panel-title">岗位信息</h3>
                </div>
                <div class="panel-body">
                    <div class="form-inline">
                        <div class="row">
                            <div class="col-sm-12 table-toolbar-left">
                                <btn id="btn_add_post" class="btn btn-primary btn-sm">
                                    <i class="ti-plus"> </i> 新增岗位
                                </btn>
                            </div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table id="post_table"></table>
                        <div id="post_page"></div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<script>

    $(function(){

        init_grid_post();

    });

    function init_grid_post() {

        __init_jqgrid('post_table', 'post_page',
                '<%=path%>/main/post/list',
                ['序号', '岗位名称', '岗位描述', '操作',  'id'],
                [
                    {name: 'sortNo', index: 'sortNo', width: 20, sortable: false, search: false},
                    {name: 'name', index: 'name', width: 60, sortable: false, stype: 'text', searchoptions: {sopt: ['cn']}},
                    {name: 'description', index: 'description', width: 100, sortable: false, search: false},
                    {name: 'act', index: 'act', width: 150, fixed: true, sortable: false, search: false, formatter: actFormat},
                    {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
                ],
                true
        );

    }

    function actFormat(cellvalue, options, rowObject){
        var id = rowObject.id;
        cellvalue = '<button class="btn btn-default btn-xs" data-original-title="编辑" onclick="fn_post_edit(\'' + id + '\');"><i class="fa fa-pencil-square-o"> </i> 编辑</button> ';
        cellvalue += '<button class="btn btn-danger btn-xs" data-original-title="删除" onclick="fn_post_method(\'Delete\',\'' + id + '\');"><i class="fa fa-trash-o"> </i> 删除</button>';

        return cellvalue;
    }

    function stateFormat(cellvalue, options, rowObject){
        var id = rowObject.id;
        if (cellvalue == 'Enable') {
            cellvalue = '<button class="btn btn-success btn-xs btn-rounded" type="button" onclick="fn_post_method(\'Disenable\',\'' + id + '\');"><i class="fa fa-check-circle-o"></i> 启用中</button>'
        } else {
            cellvalue = '<button class="btn btn-warning btn-xs btn-rounded" type="button" onclick="fn_post_method(\'Enable\',\'' + id + '\');"><i class="fa fa-ban"></i> 已禁用</button>'
        }
        return cellvalue;
    }

    $('#btn_add_post').click(function() {
        __open_dialog_form('新增岗位', '<%=path%>/main/post/create', function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 刷新grid
                    $('#post_table').trigger('reloadGrid');
                } else {
                    dialogRef.enableButtons();
                }
            };
            if (fn_post_input_save(callback) === false) {
                dialogRef.enableButtons();
            }
        });
        return false;
    });

    function fn_post_edit(id) {
        __open_dialog_form('编辑岗位信息', '<%=path%>/main/post/update/'+id, function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 刷新grid
                    $('#post_table').trigger('reloadGrid');
                } else {
                    dialogRef.enableButtons();
                }
            };
            if (fn_post_input_save(callback) === false) {
                dialogRef.enableButtons();
            }
        });
    }

    function fn_post_method(method, id) {
        var message = '确定执行此操作么';
        if (method == 'Delete') {
            message = '确定删除么';
        } else if (method == 'Enable') {
            message = '确定启用么';
        } else if (method == 'Disenable') {
            message = '确定禁用么';
        }
        __confirm_dialog(null, message, function () {
            var url = '<%=path%>/main/post/method/'+method+'/'+id;
            __ajax_post(url, null, function(data) {
                __toastr(data);
                if(data.state == 1) {
                    // 刷新grid
                    $('#post_table').trigger('reloadGrid');
                }
            });
        });
    }

</script>